//
//  SQLoginViewController.m
//  pai
//
//  Created by pro on 14-10-30.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQLoginViewController.h"
#import "SQRegisterViewController.h"
#import "SQFindPasswordViewController.h"
#import "SQMyViewController.h"
#import "SQRechargeViewController.h"
#import "Global.h"

@interface SQLoginViewController ()

@end

@implementation SQLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"登录"];
    
    self.navigationController.tabBarController.tabBar.hidden = YES;
    
    self.savePasswordFlag = false;
    
    //注册按钮
    UIButton* registerbutton = [[UIButton alloc]initWithFrame:CGRectMake(240, 15, 50, 18)];
    [registerbutton setTitle:@"注册" forState:UIControlStateNormal];
    [registerbutton addTarget:self action:@selector(onActionRegister:) forControlEvents:UIControlEventTouchUpInside];
    [self.MyNavigationBar addSubview:registerbutton];
    
    [self.accountLabel addTarget:self action:@selector(moveDownTextField:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.passwordLabel addTarget:self action:@selector(moveDownTextField:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.passwordLabel setSecureTextEntry:YES];
    
    [self initcheckbox];
    
}
-(void)moveDownTextField:(UITextField* )text
{
    [text resignFirstResponder];
}
-(void)onActionRegister:(UIButton*)button
{
    SQRegisterViewController* rvc = [[SQRegisterViewController alloc]init];
    [self.navigationController pushViewController:rvc animated:YES];
}
-(void)initcheckbox
{
    self.checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect checkboxRect = CGRectMake(15,225,50,50);
    [self.checkbox setFrame:checkboxRect];
    
    [self.checkbox setImage:[UIImage imageNamed:@"checkbox_off.png"] forState:UIControlStateNormal];
    [self.checkbox setImage:[UIImage imageNamed:@"checkbox_on.png"] forState:UIControlStateSelected];
    
    [self.checkbox addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.checkbox];
}
-(void)checkboxClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)onActionBack:(UIButton*)button
{
    self.navigationController.tabBarController.tabBar.hidden = NO;
    NSLog(@"%@",[self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -2)]);
    if([[self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -2)] isKindOfClass:[SQMyViewController class]]||[[self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -2)] isKindOfClass:[SQRechargeViewController class]])
    {
        //如果是支付、我的，跳转到首页
        [self.tabBarController setSelectedIndex:0];
        [self.navigationController popViewControllerAnimated:NO];
        
    }
    else{
          [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    [super touchesBegan:touches withEvent:event];
}
- (IBAction)doActionFindPassword:(id)sender {
    SQFindPasswordViewController* fpvc = [[SQFindPasswordViewController alloc]init];
    [self.navigationController pushViewController:fpvc animated:YES];
}

- (IBAction)doActionLogin:(id)sender {

   [self onlogin];
}
-(void)onlogin
{
    user_login_t login;
    login.base.msgtype = 1011;
    login.base.len = sizeof(login);
    login.base.error = 0;
    login.devicetype = 0;
    strcpy(login.userid, [self.accountLabel.text UTF8String]);
    strcpy(login.userpwd, [self.passwordLabel.text UTF8String]);
    strcpy(login.appver, [VERSION UTF8String]);
    if ([Global instance].devicetoken) {
        strcpy(login.devicetoken, [[Global instance].devicetoken UTF8String]);
    }
    else{
        strcpy(login.devicetoken, "");
    }
    
    NSMutableData* msgData = [NSMutableData data];
    [msgData appendBytes:&login length:sizeof(login)];
    
    [super sendData:msgData tag:1 withHUD:YES];
}
//读取数据
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSString *aStr=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",aStr);
    user_login_ack_t loginack;
    [data getBytes:&loginack length:sizeof(loginack)];
    NSLog(@"data len:%d",data.length);
    NSLog(@"%d",loginack.base.error);
    NSLog(@"%d",loginack.base.len);
    if(loginack.base.error==ACS_SUCCESS || loginack.base.error==3070){
        [self.HUD showAnimated:YES whileExecutingBlock:^{
            self.HUD.labelText = @"登录成功";
            sleep(2);
            [self.HUD show:NO];
        } completionBlock:^{
            UserInfo* userinfo = [[UserInfo alloc] init];
            userinfo.username = [NSString stringWithUTF8String:loginack.usename];
            userinfo.password = self.passwordLabel.text;
            userinfo.mobile = [NSString stringWithUTF8String:loginack.cellphone];
            userinfo.points = loginack.points;
            userinfo.money = loginack.ballance;
            NSLog(@"point:%d",userinfo.points);
            NSLog(@"money:%d",userinfo.money);
            [Global instance].userinfo = userinfo;
            
            if (self.checkbox.selected) {
                [userinfo doSave];
            }
            
            if (loginack.base.error==3070) {
                NSLog(@"you xiao xi");
                [[NSNotificationCenter  defaultCenter]postNotificationName:@"ACS_ERR_UNREAD_NOTE" object:nil];
            }
            
            self.navigationController.tabBarController.tabBar.hidden = NO;
            [Global instance].logined = YES;
            [self.navigationController popViewControllerAnimated:NO];
        }];
    }else{
        //出错
        [self.HUD showAnimated:YES whileExecutingBlock:^{
            self.HUD.labelText = @"用户名或密码错误";
            sleep(2);
            [self.HUD show:NO];
        } completionBlock:^{
        }];
        return;
    }
}
@end
